package service

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"sort"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
)

var Platform = &PlatformService{}

type PlatformService struct{}

func (s *PlatformService) GetPlatformList() []*model.Platform {
	return s.GetPlatformListByPlatformIdList([]string{})
}

func (s *PlatformService) GetPlatformOne(platformId string) *model.Platform {
	data, err := dao.Platform.Where(dao.Platform.Columns.Id, platformId).FindOne()
	defErr.IfPanic(err, define.DataOptFail)

	data.InventorySeverIds = make([]int, 0)
	rel, relErr := dao.PlatformInventoryServerRel.Where(dao.PlatformInventoryServerRel.Columns.PlatformId, data.Id).
		FindArray(dao.PlatformInventoryServerRel.Columns.InventoryServerId)
	defErr.IfPanic(relErr, define.DataOptFail)
	if len(rel) > 0 {
		relArr := gconv.SliceInt(rel)
		sort.Ints(relArr)
		data.InventorySeverIds = relArr
	}
	return data
}

//获取平台列表
func (s *PlatformService) GetPlatformListByPlatformIdList(platformIdList []string) []*model.Platform {
	var (
		data = make([]*model.Platform, 0)
		//rel  = make([]*model.PlatformInventoryServerRel, 0)
		err error
	)

	if len(platformIdList) == 0 {
		data, err = dao.Platform.FindAll()
		defErr.IfPanic(err, define.DataOptFail)
	} else {
		data, err = dao.Platform.Where(g.Map{
			dao.Platform.Columns.Id: platformIdList,
		}).FindAll()
		defErr.IfPanic(err, define.DataOptFail)
	}

	for _, v := range data {
		v.InventorySeverIds = make([]int, 0)
		rel, relErr := dao.PlatformInventoryServerRel.Where(dao.PlatformInventoryServerRel.Columns.PlatformId, v.Id).
			FindArray(dao.PlatformInventoryServerRel.Columns.InventoryServerId)
		defErr.IfPanic(relErr, define.DataOptFail)
		if len(rel) > 0 {
			relArr := gconv.SliceInt(rel)
			sort.Ints(relArr)
			v.InventorySeverIds = relArr
		}

		v.ChannelList, err = dao.Channel.Where(dao.Channel.Columns.PlatformId, v.Id).FindAll()
		defErr.IfPanic(err, define.DataOptFail)
	}

	return data
}

//获取平台列表
func (s *PlatformService) GetPlatformListByUserId(u *model.MyadminUser) []*model.Platform {
	var list []*model.Platform
	if u.IsSuper == 1 {
		list = Platform.GetPlatformList()
	} else {
		channelList, err := dao.Channel.As("T0").Fields("DISTINCT T0.*").
			LeftJoin(dao.MyadminRoleUserRel.Table, "T1",
				"T1."+dao.MyadminRoleChannelRel.Columns.RoleId+"=T1."+dao.MyadminRoleUserRel.Columns.RoleId).
			LeftJoin(dao.MyadminRoleChannelRel.Table, "T2",
				"T0."+dao.Channel.Columns.Id+"=T2."+dao.MyadminRoleChannelRel.Columns.ChannelId).
			Where("T1."+dao.MyadminRoleUserRel.Columns.UserId+" = ?", u.Id).FindAll()
		defErr.IfPanic(err, define.DataOptFail)

		//对重复数据不进行写入
		flag := make(map[string]bool)
		for _, v := range channelList {
			_, ok := flag[v.PlatformId]
			if ok {

			} else {
				flag[v.PlatformId] = true
				platform := Platform.GetPlatformOne(v.PlatformId)
				list = append(list, platform)
			}
		}

		var channelMap = make(map[string]*model.Channel)
		for _, v := range channelList {
			channelMap[v.PlatformId] = v
		}

		for _, v := range list {
			if channelArr, has := channelMap[v.Id]; has {
				v.ChannelList = channelArr
			}
		}
	}

	return list
}
